package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntStoreInfoBean;
import cc.rengu.igas.bsps.facade.request.MchntStoreListQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntStoreListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 赵奎伍
 * @version 1.0
 * @ClassName MchntStoreListQueryService
 * @Description 商户门店列表查询
 * @date 2020/4/2
 */
public class MchntStoreListQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntStoreListQueryRequest mchntStoreListQueryRequest = new MchntStoreListQueryRequest();
        ConvertUtil.convertOutput(mchntStoreListQueryRequest);
        return mchntStoreListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntStoreListQueryRequest requestParams = (MchntStoreListQueryRequest) request;
        MchntStoreListQueryResponse responseParams = new MchntStoreListQueryResponse();
        /* 组装应答报文 */
        BizResponse<MchntStoreListQueryResponse> bizResponse = new BizResponse<>();

        String userId = requestParams.getUserId();
        String mchntNo = requestParams.getMchntNo();
        String instId = requestParams.getHeader().getInstId();
        String chanlId = requestParams.getHeader().getChanlId();
        if (!"MOBILE".equals(chanlId)) {
            if (StringUtil.isEmptyOrNull(userId)) {
                rglog.error("接口上传参数不合法,userId:<{}>", requestParams.getUserId());
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        /* 参数合法性检查 */
        if (StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("接口上传参数不合法,userId:<{}>,mchntNo:<{}>", requestParams.getUserId(), mchntNo);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        //根据机构号，商户号查询商户基本信息表
        MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("获取商户信息失败,mchntNo:<{}>", mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        if (StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntType()) || !"10".equals(mchntBaseInfo.getMchntType())) {
            rglog.error("商户非连锁商户,mchntNo:<{}>", mchntNo);
            /* 查询结果赋值 */
            responseParams.setMchntName(mchntBaseInfo.getMchntName());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setResult(responseParams);
            return bizResponse;
        }

        responseParams.setMchntName(mchntBaseInfo.getMchntName());
        List<MchntStoreInfoBean> mchntStoreInfoBeans = new ArrayList<>();
        List<MchntBaseInfo> mchntBaseInfoList = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByChainMchntNo(instId, mchntNo);
        if (!CollectionUtils.isEmpty(mchntBaseInfoList)) {
            /* 按照创建时间降序排序 */
            List<MchntBaseInfo> binInfoLists = mchntBaseInfoList.stream().sorted(Comparator.comparing(MchntBaseInfo::getCreateTime)).collect(Collectors.toList());
            Collections.reverse(binInfoLists);
            binInfoLists.forEach(item -> convMchntBaseInfo(item, mchntStoreInfoBeans));
        } else {
            rglog.error("根据该商户号查无门店列表信息!，mchntNo:<{}>", mchntNo);
        }

        /* 查询结果赋值 */
        responseParams.setMchntStoreInfoList(mchntStoreInfoBeans);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(responseParams);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntStoreListQueryResponse mchntStoreListQueryResponse = (MchntStoreListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntStoreListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param mchntBaseInfo   数据库表结构对象
     * @param mchntStoreInfos 接口对象
     */
    private void convMchntBaseInfo(MchntBaseInfo mchntBaseInfo, List<MchntStoreInfoBean> mchntStoreInfos) {
        MchntStoreInfoBean mchntStoreInfo = new MchntStoreInfoBean();
        BeanUtil.beanCopy(mchntBaseInfo, mchntStoreInfo);
        if (mchntBaseInfo != null) {
            mchntStoreInfo.setMchntStoreNo(mchntBaseInfo.getMchntNo());
            mchntStoreInfo.setMchntStoreName(mchntBaseInfo.getMchntName());
        }
        mchntStoreInfos.add(mchntStoreInfo);

    }
}
